package com.ruoyi.web.controller.retirement;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.retirement.domain.RetirementArticle;
import com.ruoyi.retirement.service.IRetirementArticleService;
import com.ruoyi.utils.alicloud.SoundOSS_Dialect;
import org.jsoup.Jsoup;
import org.jsoup.safety.Safelist;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 文章发布Controller
 * 
 * @author 曹旭
 * @date 2025-09-02
 */
@RestController
@RequestMapping("/retirement/retirementArticle")
public class RetirementArticleController extends BaseController
{
    @Autowired
    private IRetirementArticleService retirementArticleService;


    @Autowired
    private SoundOSS_Dialect soundOSS_Dialect;
    /**
     * 查询文章发布列表
     */
    @PreAuthorize("@ss.hasPermi('retirement:retirementArticle:list')")
    @GetMapping("/list")
    public TableDataInfo list(RetirementArticle retirementArticle)
    {
        startPage();
        List<RetirementArticle> list = retirementArticleService.selectRetirementArticleList(retirementArticle);
        return getDataTable(list);
    }


    /**
     * 查询文章发布列表
     */
    @GetMapping("/getList")
    public AjaxResult getList(RetirementArticle retirementArticle)
    {
        List<RetirementArticle> list = retirementArticleService.selectRetirementArticleList(retirementArticle);
        return success(list);
    }


    /**
     * 导出文章发布列表
     */
    @PreAuthorize("@ss.hasPermi('retirement:retirementArticle:export')")
    @Log(title = "文章发布", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RetirementArticle retirementArticle)
    {
        List<RetirementArticle> list = retirementArticleService.selectRetirementArticleList(retirementArticle);
        ExcelUtil<RetirementArticle> util = new ExcelUtil<RetirementArticle>(RetirementArticle.class);
        util.exportExcel(response, list, "文章发布数据");
    }

    /**
     * 获取文章发布详细信息
     */
    @PreAuthorize("@ss.hasPermi('retirement:retirementArticle:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(retirementArticleService.selectRetirementArticleById(id));
    }

    /**
     * 新增文章发布
     */
    @PreAuthorize("@ss.hasPermi('retirement:retirementArticle:add')")
    @Log(title = "文章发布", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RetirementArticle retirementArticle)
    {
        if (StringUtils.isNotEmpty(retirementArticle.getDesc())){
            //把富文本内容转换成text文字 调用阿里云语音合成  生成音频文件
            String clean = Jsoup.clean(retirementArticle.getDesc(), Safelist.none());
            String clean_text = clean.replace("&nbsp;", "");
            retirementArticle.setDescText(clean_text);
            String desc_url = soundOSS_Dialect.uploadSoundOSS(clean_text, "");
            retirementArticle.setDescUrl(desc_url);
            String replace = retirementArticle.getDesc().replace("&nbsp;", "");
            retirementArticle.setDesc(replace);
        }
        return toAjax(retirementArticleService.insertRetirementArticle(retirementArticle));
    }

    /**
     * 修改文章发布
     */
    @PreAuthorize("@ss.hasPermi('retirement:retirementArticle:edit')")
    @Log(title = "文章发布", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RetirementArticle retirementArticle)
    {
        if (StringUtils.isNotEmpty(retirementArticle.getDesc())){
            //把富文本内容转换成text文字 调用阿里云语音合成  生成音频文件
            String clean = Jsoup.clean(retirementArticle.getDesc(), Safelist.none());
            String clean_text = clean.replace("&nbsp;", "");
            retirementArticle.setDescText(clean_text);
            String desc_url = soundOSS_Dialect.uploadSoundOSS(clean_text, "");
            retirementArticle.setDescUrl(desc_url);
        }
        return toAjax(retirementArticleService.updateRetirementArticle(retirementArticle));
    }

    /**
     * 删除文章发布
     */
    @PreAuthorize("@ss.hasPermi('retirement:retirementArticle:remove')")
    @Log(title = "文章发布", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(retirementArticleService.deleteRetirementArticleByIds(ids));
    }
}
